local skel = fk.CreateSkill {
  name = "emo__tidengwj",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__tidengwj"] = "提灯握剑",
  [":emo__tidengwj"] = "摸牌阶段，明置你摸到的牌；你失去最后的明置牌后，摸1张牌并明置之，若此时处理区有【杀】，则翻倍。",

  ["$emo__tidengwj1"] = "我的灯将净化邪恶！",
  ["$emo__tidengwj2"] = "我的眼将找出真相！",
}

local DIY = Pcall(require, "packages/diy_utility/diy_utility")

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and not player:isKongcheng() and player.phase == Player.Draw then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.player_cards[Player.Hand], info.cardId) then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local ids = {}
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(player.player_cards[Player.Hand], info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
    DIY.showCards(player, ids)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local ids = ((data.extra_data or {})[skel.name] or {})[player]
      if not ids then return false end
      local copy = table.simpleClone(ids) -- 每个都验证一次是否失去，以免被防止移动干扰
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            table.removeOne(copy, info.cardId)
          end
        end
      end
      if #copy == 0 then
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local num = 1
    if table.find(player.room.processing_area, function (id)
      return Fk:getCardById(id).trueName == "slash"
    end) then
      num = 2
    end
    local ids = player:drawCards(num, skel.name)
    if player.dead or #ids == 0 then return end
    DIY.showCards(player, ids)
  end,
})

skel:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true) and not player:isKongcheng()
  end,
  on_refresh = function (self, event, target, player, data)
    local shown = DIY.getShownCards(player)
    if #shown == 0 then return end
    local copy = table.simpleClone(shown)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          table.removeOne(copy, info.cardId)
        end
      end
    end
    if #copy > 0 then return end
    data.extra_data = data.extra_data or {}
    data.extra_data[skel.name] = data.extra_data[skel.name] or {}
    data.extra_data[skel.name][player] = shown
  end,
})

return skel
